# -*- coding: utf-8 -*-
"""
Created on Fri Nov  9 00:54:27 2018

@author: geyang
@e-mail: 
"""

class LNode:
    def __init__(self, element, next_ = None):
        self.element = element
        self.next = next_
        
        
class LinkedStack:
    
    def __init__(self):
        self.top = None  # top represents the head of the stack
        self.length = 0
        
    def is_empty(self):
        if self.top is None:
            return True
        return False
    
    def push(self, element):
        """insert element at head of the stack
        """
        node = LNode(element)
        if self.top is None:
            self.top = node
        else:
            node.next = self.top
            self.top = node
        self.length += 1
        
    def pop(self):
        """delete element at head of the stack
        """
        if self.top is None:
            raise ValueError("stack empty!")
        result = self.top.element
        self.top = self.top.next
        self.length -= 1
        return result
        
    def top(self):
        if self.top is None:
            raise ValueError("stack empty!")
        return self.top.element
    
    def print_all(self):
        result = []
        p = self.top
        while p is not None:
            result.append(p.element)
            p = p.next
        return result